package br.furb.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import br.furb.controller.ItemEstoqueController;
import br.furb.util.Imagem;
import br.furb.util.Observer;
import br.furb.util.Subject;

public class CadastroItemEstoque extends JDialog implements Subject {

	private final JPanel contentPanel = new JPanel();
	private JButton btnCancelar;
	private JButton btnSalvar;
	private ItemEstoqueController itemEstoqueController;
	private JDialog me;
	private JTextField txtDescricao;
	private JSpinner quantidade;
	private JFormattedTextField txtValor;
	private JButton btnLimpar;
	private List<Observer> observers = new ArrayList<Observer>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CadastroItemEstoque dialog = new CadastroItemEstoque(null);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CadastroItemEstoque(JFrame owner) {
		super(owner, true);
		setSize(new Dimension(330, 180));
		setTitle("Novo Item de Estoque");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(owner);
		setResizable(false);
		
		me = this;
		itemEstoqueController = new ItemEstoqueController(this);
		
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o:");
		lblDescrio.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		txtDescricao = new JTextField();
		lblDescrio.setLabelFor(txtDescricao);
		txtDescricao.setColumns(10);
		
		JLabel lblQuantidade = new JLabel("Quantidade:");
		lblQuantidade.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		quantidade = new JSpinner();
		lblQuantidade.setLabelFor(quantidade);
		
		JLabel lblValor = new JLabel("Valor:");
		lblValor.setFont(new Font("Tahoma", Font.BOLD, 12));
		
//		NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat("#.##"));
//		NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat("R$ #,###.##"));
//		NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.##"));
//		
//		defaultFormatter.setValueClass(Double.class);
//		displayFormatter.setValueClass(Double.class);
//		editFormatter.setValueClass(Double.class);
//		
//		DefaultFormatterFactory salaryFactory = new DefaultFormatterFactory(defaultFormatter,displayFormatter,editFormatter);
		
		txtValor = new JFormattedTextField();
//		txtValor.setFormatterFactory(salaryFactory);
		lblValor.setLabelFor(txtValor);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setAutoCreateContainerGaps(true);
		gl_contentPanel.setAutoCreateGaps(true);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtDescricao, GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
						.addComponent(lblDescrio)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(lblQuantidade)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(quantidade, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblValor)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtValor, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDescrio)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtDescricao, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(quantidade, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblQuantidade)
						.addComponent(lblValor)
						.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(141, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				btnLimpar = new JButton("Limpar");
				btnLimpar.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnLimpar.setIcon(Imagem.icone("reset.png"));
				btnLimpar.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if (JOptionPane.showConfirmDialog(me, "Deseja limpar os campos?", "", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
							limparCampos();
						}
					}
				});
				buttonPane.add(btnLimpar);
				
				btnSalvar = new JButton("Salvar");
				btnSalvar.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnSalvar.setActionCommand("salvar");
				btnSalvar.setIcon(Imagem.icone("confirm.png"));
				btnSalvar.addActionListener(this.itemEstoqueController);
				buttonPane.add(btnSalvar);
				getRootPane().setDefaultButton(btnSalvar);
			}
			{
				btnCancelar = new JButton("Cancelar");
				btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnCancelar.setActionCommand("cancelar");
				btnCancelar.setIcon(Imagem.icone("cancel.png"));
				btnCancelar.addActionListener(this.itemEstoqueController);
				buttonPane.add(btnCancelar);
			}
		}
	}

	@Override
	public void addObserver(Observer o) {
		observers.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		observers.remove(o);
	}

	@Override
	public void removeObserver(int i) {
		observers.remove(i);
	}

	@Override
	public void notifyObservers() {
		for (Observer o : observers) {
			o.update(itemEstoqueController.getModel());
		}
	}
	
	@Override
	public void dispose() {
		limparCampos();
		super.dispose();
	}
	
	private void limparCampos() {
		txtDescricao.setText("");
		txtValor.setText("");
		quantidade.setValue(0);
	}
	
	public String getDescricao() {
		return txtDescricao.getText();
	}
	
	public int getQuantidade() {
		return (Integer) quantidade.getValue();
	}
	
	public double getValor() {
		return Double.parseDouble(txtValor.getText().replace(',', '.'));
	}
}